<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 500px;
            height: 500px;
            background-color: pink;
            margin: 0 auto;
        }
        
        .box_title {
            width: 100%;
            height: 100px;
            display: flex;
            justify-content: space-around;
            line-height: 100px;
        }
        
        .title_item {
            width: 150px;
            background-color: blueviolet;
            text-align: center;
        }
        
        .title_item:hover {
            cursor: pointer;
        }
        
        .box_content {
            position: relative;
            width: 100%;
            height: 400px;
        }
        
        .content_item {
            position: absolute;
            left: 0;
            top: 0;
            font-size: 100px;
            width: 100%;
            height: 100%;
            display: none;
            text-align: center;
        }
        
        .content_active {
            display: block;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="box_title">
            <div class="title_item">标题1</div>
            <div class="title_item">标题2</div>
            <div class="title_item">标题3</div>
            <div class="title_item">标题4</div>
        </div>
        <div class="box_content">
            <div class="content_item content_active">内容1</div>
            <div class="content_item">内容2</div>
            <div class="content_item">内容3</div>
            <div class="content_item">内容4</div>
        </div>
    </div>

    <script>
        var box_titles = document.querySelectorAll('.title_item');
        console.log(box_titles);
        var box_contents = document.querySelectorAll('.content_item');

        var oldIndex = 0; // 旧下标
        box_titles.forEach(function(ele, index) {
            ele.addEventListener('click', function() {
                // for (let j = 0; j < box_titles.length; j++) {
                //     box_contents[j].classList.remove('content_active');

                // }
                // console.log(box_contents);
                // box_contents[index].classList.add('content_active');
                // console.log(box_contents);
                console.log(index);
                box_contents[oldIndex].classList.remove('content_active');
                box_contents[index].classList.add('content_active');
                oldIndex = index; // 记录上次的点击下标
            })
        });
    </script>
</body>

</html>